<template>
    <div class="login_container">
        <div class="login_box">
            <div class="logo">
                <img src="../../assets/img/Logo.jpg" alt="">
            </div>
            <el-form class="loginFrom" ref="LoginFrom" :rules="rules" :model="LoginFrom">
                 <el-form-item prop="username">
                    <el-input placeholder="请输入账号" prefix-icon="el-icon-user" v-model.number="LoginFrom.username"></el-input>
                 </el-form-item>
                 <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="LoginFrom.password"></el-input>
                 </el-form-item>
                 <el-form-item prop="password2">
                    <el-input type="password" placeholder="确认密码" prefix-icon="el-icon-lock" v-model="LoginFrom.password2"></el-input>
                 </el-form-item>
                 <el-form-item class="btns">
                    <el-button type="primary" @click="Login('LoginFrom')">注册</el-button>
                    <el-button type="info" @click="resetForm('LoginFrom')">重置</el-button>
                 </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('账号不能为空'))
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('账号格式只能为数字'))
        }
        if ((value + '').length < 3 || (value + '').length > 8) {
          callback(new Error('账号必须要 3 到 8 位'))
        } else {
          callback()
        }
      }, 1000)
    }
    var Password = (rule, value, callback) => {
      if (!value) {
        callback(new Error('密码不能为空'))
      } else {
        if (value.length < 6 || value.length > 13) {
          callback(new Error('密码长度只能为 6 到 13 位'))
        }
        callback()
      }
    }
    var CheckPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('密码不能为空'))
      } else {
        if (value !== this.LoginFrom.password) {
          callback(new Error('两次输入密码不符'))
        }
        callback()
      }
    }
    return {
      LoginFrom: {
        username: '',
        password: '',
        password2: ''
      },
      rules: {
        username: [
          { validator: checkUsername, trigger: 'blur' }
        ],
        password: [
          { validator: Password, trigger: 'blur' }
        ],
        password2: [
          { validator: CheckPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    Login (formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) return
        const { data: res } = this.$http.post('', this.LoginFrom)
        if (res.meta.status !== 200) return this.$message.error('注册失败')
        this.$message.success('注册成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/login')
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 350px;
    background-color: aliceblue;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    .logo{
        height: 130px;
        width: 130px;
        border: 1px solid white;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: white;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: white;
        }
    }
}
.btns{
    display: flex;
    justify-content: flex-end;
}
.loginFrom{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
}
</style>
